import axios from 'axios'
import store from '@/store'
import { setToken } from '@/libs/util';
import { Notice } from 'iview';
// import { Spin } from 'iview'
const addErrorLog = errorInfo => {
  const { statusText, status, request: { responseURL } } = errorInfo
  let info = {
    type: 'ajax',
    code: status,
    mes: statusText,
    url: responseURL
  }
  // if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info)
}

class HttpRequest {
  constructor (baseUrl = baseURL) {
    this.baseUrl = baseUrl
    this.queue = {}
  }
  getInsideConfig () {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        //
      }
    }
    return config
  }
  destroy (url) {
    delete this.queue[url]
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }
  interceptors (instance, url) {
    // 请求拦截
    instance.interceptors.request.use(config => {
      // 添加全局的loading...
      if (!Object.keys(this.queue).length) {
        // Spin.show() // 不建议开启，因为界面不友好
      }
      // if (url !== `https://pdd.supai.net/general/supers/login`) {
      //   config.headers['AuthToken'] = getToken()
      // }
      this.queue[url] = true
      return config
    }, error => {
      return Promise.reject(error)
    })
    // 响应拦截
    instance.interceptors.response.use(res => {
      // console.log(res);
      this.destroy(url)
      const { data, status } = res;
      if (data.code === 1) {
        setToken('')
        location.reload();
      };
      if (data.code === 3) {
        Notice.warning({
          title: '您的权限不足,请联系管理员！！'
        })
        return;
    }
    if (res.data.code === 4) {
      Notice.warning({
        title: res.data.msg
      });
      return;
    }
    if (res.data.code === 400) {
      Notice.warning({
        title: '错误提示',
        desc: res.data.msg
      })
      return;
    }
    if (res.data.code === 401) {
      Notice.warning({
        title: '错误提示',
        desc: res.data.msg

      })
      return;
    }
    if (res.data.code === 500) {
      Notice.warning({
        title: '提示',
        desc: '系统错误:'+res.data.msg
      })
    }
      return { data, status }
      // if (data.code === 0 || data.code === '0') {
      //   return { data, status }
      // } else {
      //   console.log(data.msg)
      //   this.$Message.error(data.msg)
      //   return null
      // }
    }, error => {
      this.destroy(url)
      console.log(error)
      let errorInfo = error.response
      if (!errorInfo) {
        const { request: { statusText, status }, config } = JSON.parse(JSON.stringify(error))
        errorInfo = {
          statusText,
          status,
          request: { responseURL: config.url }
        }
      }
      addErrorLog(errorInfo)
      return Promise.reject(error)
    })
  }
  request (options) {
    const instance = axios.create()
    options = Object.assign(this.getInsideConfig(), options)
    this.interceptors(instance, options.url)
    return instance(options)
  }
}
export default HttpRequest
